package com.zeroext.show.dao.impl;

import java.util.List;

import org.hibernate.Session;

import com.zeroext.dao.common.BaseDAO;
import com.zeroext.show.dao.TopoDAO;
import com.zeroext.show.entity.NetworkTopo;
import com.zeroext.show.entity.TopoElement;

public class TopoDAOImpl extends BaseDAO implements TopoDAO {

    public void addTopoElemtn(NetworkTopo topo, TopoElement el) {
        Session session = this.getHibernateSession();
        topo.getElements().add(el);
        session.save(el);
        session.update(topo);
    }

    public void deleteTopoElement(NetworkTopo topo, TopoElement el) {
        Session session = this.getHibernateSession();
        topo.getElements().remove(el);
        session.delete(el);
        session.update(topo);
    }

    public NetworkTopo getNetworkTopo(Long id) {
        Session session = this.getHibernateSession();
        return (NetworkTopo) session.get(NetworkTopo.class, id);
    }

    public TopoElement getTopoElement(Long id) {
        Session session = this.getHibernateSession();
        return (TopoElement)session.get(TopoElement.class, id);
    }

    public NetworkTopo saveNetworkTopo(NetworkTopo topo) {
        this.getHibernateSession().saveOrUpdate(topo);
        return topo;
    }

    public TopoElement saveTopoElement(TopoElement el) {
        return (TopoElement)this.getHibernateSession().save(el);
    }

    public void deleteNetworkTopo(Long id) {
        Session session = this.getHibernateSession();
        session.delete(id);
    }

    @SuppressWarnings("unchecked")
    public List<NetworkTopo> searchNetworkTopo() {
        Session session = this.getHibernateSession();
        return session.createCriteria(NetworkTopo.class).list();
    }

}
